package com.catbad.controller;

import com.catbad.annotations.Auth;
import com.catbad.domain.FunctionList;
import com.catbad.domain.User;
import com.catbad.services.FilmServices;
import com.catbad.services.FunctionListServices;
import com.catbad.services.UserServices;
import com.catbad.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Controller
public class ReceptionController {
    @Autowired
    UserServices userServices;
    @Autowired
    FunctionListServices functionListServices;
    @Autowired
    FilmServices filmServices;
    /**
     *首页
     * @return
     */
    @RequestMapping("/")
    public String index(Model model){
        //获得影片
        model.addAttribute("filmList",filmServices.getAllFilm());
        return "user/home";
    }
    /**
     * 平台管理首页
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("admin")
    public String adminPage(){
        return "admin/admin";
    }
    /**
     * 影院个人管理页面
     * @return
     */
    @Auth("com.catbad.personnel.room")
    @RequestMapping("personnel")
    public String personnelPage(){
        return "personnel/personnel";
    }
    /**
     * 登录
     * @param uaccount
     * @param upass
     * @param request
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public String login(String uaccount, String upass, HttpServletRequest request){
        String pass = null;
        try {
            pass = Tools.getMd5(upass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        User user = userServices.login(uaccount,pass);
        if(user!=null){
            //存储用户个人信息
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            List<FunctionList> functionList= functionListServices.getFunctionList(user.getRid());
            //存储菜单信息
            session.setAttribute("functionList",functionList);
            Set<String> auths = new HashSet<>();
            //存储用户权限字符串
            for(FunctionList Function:functionList) {
                auths.add(Function.getFauth());
            }
            session.setAttribute("auths",auths);
            //返回用户对应的页面
            if(user.getRid()==1) {
                return "admin";
            }else if(user.getRid()==2){
                return "personnel";
            }else if(user.getRid()==3){
                return "home";
            }
        }
        return null;
    }
    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public Integer register(User user){
        user.setUdeleteFlag("1");
        user.setUimg("ht.img");
        user.setRid(3L);
        user.setAge(18);
        user.setSex("1");
        try {
            String pass = Tools.getMd5(user.getUpass());
            user.setUpass(pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userServices.register(user);
    }
    /**
     * 修改用户信息
     * @param user
     * @param request
     * @return
     */
    @Auth("com.catbad.user.update")
    @RequestMapping("userUpdate")
    @ResponseBody
    public String userUpdate(User user,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user1 = (User)session.getAttribute("user");
        //将数据转入,由于可能密码没有修改，所以交给service处理
        //
        user.setUpass(user.getUpass());
        user.setUaccount(user1.getUaccount());
        user.setUid(user1.getUid());
        user.setUimg(user1.getUimg());
        user.setRid(3L);
        user.setUdeleteFlag("1");
        userServices.setUser(user);
        session.setAttribute("user",user);
        return "";
    }

    /**
     * 用户修改头像
     * @param file
     * @return
     */
    @Auth("com.catbad.user.update")
    @RequestMapping("userUpdateImg")
    @ResponseBody
    public String userUpdateImg(MultipartFile file,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String oldImgPath = user.getUimg();
        try {
            if(!file.isEmpty()){
                //删除原来照片，创建新的图片
                String path = session.getServletContext().getRealPath("/");
                String imgName = UUID.randomUUID()+file.getOriginalFilename();
                String imgPath = path+"img\\"+imgName;
                oldImgPath = path+"img\\"+oldImgPath;
                File newImg = new File(imgPath);
                File oldImg = new File(oldImgPath);
                if(oldImg.exists()){
                    oldImg.delete();
                }
                file.transferTo(newImg);
                user.setUimg(imgName);
                userServices.setUser(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "1";
    }
    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("exit")
    @ResponseBody
    public String exit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        session.removeAttribute("auths");
        session.removeAttribute("functionList");
        return "";
    }
    /**
     * 后台系统退出
     * @param request
     * @return
     */
    @RequestMapping("backExit")
    public String backExit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        session.removeAttribute("auths");
        session.removeAttribute("functionList");
        return "redirect:/";
    }
    /**
     * 影城介绍
     * @return
     */
    @RequestMapping("/we")
    public String we(){
        return "user/jies";
    }
}
